import { createRouter, createWebHistory } from 'vue-router'

// 路由实例创建与配置
// createWebHistory: 使用HTML5 history模式
// routes: 定义应用所有路由规则
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", redirect: '/Home' }, // 默认路由重定向到首页
    {
      path: '/Login',
      meta: { title: 'Login' },
      component: () => import('../views/Login.vue'),
      children: []
    },
    { 
      path: '/Home',
      meta: { title: 'Home' },
      component: () => import('../views/Home.vue'),
      children: [
        // 教室管理
        { path: '/classroom/categories', component: () => import('../views/classroom/ClassroomCategories.vue'), meta: { title: '教室分类' }},
        { path: '/classroom/list', component: () => import('../views/classroom/ClassroomList.vue'), meta: { title: '教室列表' }},
        { path: '/classroom/information', component: () => import('../views/classroom/ClassroomInformation.vue'), meta: { title: '教室信息' }},
        { path: '/classroom/maintenance', component: () => import('../views/classroom/MaintenanceInformation.vue'), meta: { title: '维修信息' }},
        { path: '/classroom/statistics', component: () => import('../views/classroom/ClassroomStatistics.vue'), meta: { title: '数据统计' }},
        // 预约管理
        { path: '/reservation/list', component: () => import('../views/reservation/ReservationList.vue'), meta: { title: '预约列表' }},
        { path: '/reservation/information', component: () => import('../views/reservation/ReservationInformation.vue'), meta: { title: '预约信息' }},
        { path: '/reservation/statistics', component: () => import('../views/reservation/ReservationStatistics.vue'), meta: { title: '预约统计' }},
        // 审批管理
        { path: '/approval/list', component: () => import('../views/approval/ApprovalList.vue'), meta: { title: '审批列表' }},
        { path: '/approval/information', component: () => import('../views/approval/ApprovalInformation.vue'), meta: { title: '审批信息' }},
        { path: '/approval/statistics', component: () => import('../views/approval/ApprovalStatistics.vue'), meta: { title: '审批统计' }},
        // 维护管理
        { path: '/maintenance/list', component: () => import('../views/maintenance/MaintenanceList.vue'), meta: { title: '维护列表' }},
        { path: '/maintenance/information', component: () => import('../views/maintenance/MaintenanceInformation.vue'), meta: { title: '维护信息' }},
        { path: '/maintenance/statistics', component: () => import('../views/maintenance/MaintenanceStatistics.vue'), meta: { title: '维护统计' }},
        // 用户管理
        { path: '/user/list', component: () => import('../views/user/UserList.vue'), meta: { title: '用户列表' }},
        { path: '/user/information', component: () => import('../views/user/UserInformation.vue'), meta: { title: '用户信息' }},
        { path: '/user/statistics', component: () => import('../views/user/UserStatistics.vue'), meta: { title: '用户统计' }},
        // 日志管理
        { path: '/log/list', component: () => import('../views/log/LogList.vue'), meta: { title: '日志列表' }}
      ]
    },
    { 
      path: '/:pathMatch(.*)*', 
      redirect: '/404', 
      meta: { title: '404 Not Found' }
    } // 通配符路由，匹配所有未定义路由
  ]
})

// 全局前置守卫
// 作用：在路由跳转前执行，此处用于动态设置页面标题
router.beforeEach((to, from, next) => {
    // 设置页面标题为路由元信息中的title
    document.title = to.meta.title;
    next(); // 必须调用next()才能继续路由跳转
})

export default router